﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using System.Web;
using Antu.WorkFlowFactory;
using System.Data;
using System.Configuration;

namespace WebSys.UI.OfficeFlow
{
    public class InstanceSearchListSkin : MPBasicTemplate
    {
        FlowFactoryProvider _FlowFactoryProvider = (FlowFactoryProvider)ProviderService.ProviderServiceMange.GetProvider("FlowFactoryProvider");
        string iid, taskname, flowtype, ksdate, jzdate, isbj, notbj,ajh,qh,xmmc,zsjdh,fwzl,bzsr;
        CustomGridView AnJianGridView;

        protected string UserId
        {
            get { if (base.Page.Session["UserId"] != null) { return base.Page.Session["UserId"].ToString(); } else { return string.Empty; } }
            set { base.Page.Session["UserId"] = value; }
        }

        protected override void OnLoad(EventArgs e)
        {
            LoginHelper.CheckLogin();
            InitControl();
            base.OnLoad(e);
            if (!base.Page.IsPostBack)
            {
                Initcy();
            }
        }

		private void InitControl()
		{
			flowtype = Antu.Utilities.UtilitiesPage.GetStringPar("flowtype", "");
			iid = Antu.Utilities.UtilitiesPage.GetStringPar("iid", "");
			taskname = Antu.Utilities.UtilitiesPage.GetStringPar("taskname", "");
			ksdate = Antu.Utilities.UtilitiesPage.GetStringPar("ksdate", "");
			jzdate = Antu.Utilities.UtilitiesPage.GetStringPar("jzdate", "");
			isbj = Antu.Utilities.UtilitiesPage.GetStringPar("isbj", "");
			notbj = Antu.Utilities.UtilitiesPage.GetStringPar("notbj", "");
			ajh = Antu.Utilities.UtilitiesPage.GetStringPar("ajh", "");
			AnJianGridView = (CustomGridView)FindControl("AnJianGridView");
			AnJianGridView.OnLoadData += new EventHandler(AnJianGridView_OnLoadData);
			AnJianGridView.RowDataBound += new GridViewRowEventHandler(AnJianGridView_RowDataBound);
			qh = Antu.Utilities.UtilitiesPage.GetStringPar("qh", "");
			xmmc = Antu.Utilities.UtilitiesPage.GetStringPar("xmmc", "");
			zsjdh = Antu.Utilities.UtilitiesPage.GetStringPar("zsjdh", "");
			fwzl = Antu.Utilities.UtilitiesPage.GetStringPar("fwzl", "");
			bzsr = Antu.Utilities.UtilitiesPage.GetStringPar("bzsr", "");
		}

        private void AnJianGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.Header:
                    break;
                case DataControlRowType.DataRow:
                    Label status = (Label)e.Row.FindControl("status");
                    DataRowView drv = (DataRowView)e.Row.DataItem;
                    string drvstatus = drv["status"].ToString();
                    if (drvstatus == "1")
                    {
                        status.Text = "办理中";
                    }
                    else if (drvstatus == "2")
                    {
                        status.Text = "办结";
                    }
                    else if (drvstatus == "0")
                    {
                        status.Text = "挂起";
                    }
                    else if (drvstatus == "-1")
                    {
                        status.Text = "中止";
                    }
                    break;
            }
        }

        private void AnJianGridView_OnLoadData(object sender, EventArgs e)
        {
            Initcy();
        }

		private void Initcy()
		{
            string query = string.Empty;
            string OfficeSearch = ConfigurationManager.AppSettings["OfficeSearch"].ToString();
            if (OfficeSearch.IndexOf(UserId) != 0)
            {
                query = @"  and w.wname in (select distinct wname
          from st_workflow w, st_instance i, st_work_item_stack ws
         where w.wid = i.wid  and ws.userid='" + UserId + @"' and i.iid = ws.iid ) and w.wname in(" + ConfigurationManager.AppSettings["ZSAnJianType"].ToString() + ")";
            }
            if (!string.IsNullOrEmpty(iid))
			{
				query += " and i.iid like '%" + iid + "%' ";
			}
			if (!string.IsNullOrEmpty(ajh))
			{
				query += @" and ((i.iid in (select iid from zs_case_register where CASEID like '%" +_FlowFactoryProvider.GetCaseIdLike(ajh) + @"%')) or 
                    (i.iid like '%" + ajh + "%')) ";
			}
			if (!string.IsNullOrEmpty(taskname))
			{
				query += " and i.name like '%" + taskname + "%' ";
			}
			if (!string.IsNullOrEmpty(ksdate))
			{
				query += " and i.accepted_time > to_date('" + ksdate + " 00:00:00','yyyy-mm-dd hh24:mi:ss') ";
			}
			if (!string.IsNullOrEmpty(jzdate))
			{
				query += " and i.accepted_time <= to_date('" + jzdate + " 23:59:59','yyyy-mm-dd hh24:mi:ss') ";
			}
			if (!string.IsNullOrEmpty(isbj) && !string.IsNullOrEmpty(notbj))
			{
				if (isbj == "1" && notbj == "1")
				{

				}
				else if (isbj == "0" && notbj == "1")
				{
					query += " and (i.status=0 or i.status=1) ";
				}
				else if (isbj == "1" && notbj == "0")
				{
					query += " and (i.status=2 or i.status=-2) ";
				}
			}
			else if (!string.IsNullOrEmpty(isbj) && string.IsNullOrEmpty(notbj))
			{
				if (isbj == "1")
				{
					query += " and (i.status=2 or i.status=-2) ";
				}
			}
			else if (string.IsNullOrEmpty(isbj) && !string.IsNullOrEmpty(notbj))
			{
				if (notbj == "1")
				{
					query += " and (i.status=0 or i.status=1) ";
				}
			}

			if (!string.IsNullOrEmpty(flowtype) && flowtype != "全部")
			{
				query += " and w.wname='" + flowtype + "' ";
			}

			if (!string.IsNullOrEmpty(qh) && qh != "所有")
			{
				query += @"and i.iid in (
select iid from xt_instance_ext where 关联ID in (
select id from zs_zzbcjfenhu where pid in (
select id from zs_prono where pid in (
select id from zs_project where 区属='" + qh + "')))) ";
			}

			if (!string.IsNullOrEmpty(xmmc))
			{
				query += @"and i.iid in (
select iid from xt_instance_ext where 关联ID in (
select id from zs_zzbcjfenhu where pid in (
select id from zs_prono where pid in (
select id from zs_project where 项目名称 like '%" + xmmc + "%'))))";
			}
			if (!string.IsNullOrEmpty(zsjdh))
			{
				query += @"and i.iid in (
select iid from xt_instance_ext where 关联ID in (
select id from zs_zzbcjfenhu where pid in (
select id from zs_prono where 征收许可证号 like '%" + zsjdh + "%' or 征收许可证号 like '%"+ReturnString2(zsjdh)+"%')))";
			}
			if (!string.IsNullOrEmpty(fwzl))
			{
				query += @"and i.iid in (
select iid from xt_instance_ext where 关联ID in (
select id from zs_zzbcjfenhu where zspid in (
select id from zs_zsperson where 房屋坐落 like '%" + fwzl + "%')))";
			}
			if (!string.IsNullOrEmpty(bzsr))
			{
				query += @"and i.iid in (
select iid from xt_instance_ext where 关联ID in (
select id from zs_zzbcjfenhu where zspid in (
select id from zs_zsperson where 产权人 like '%" + bzsr + "%')))";
			}

			DataTable dt = _FlowFactoryProvider.NewGetUserRightWorkFlow(query,UserId);
			AnJianGridView.DataSource = dt;
			AnJianGridView.RecordCount = dt.Rows.Count;
			AnJianGridView.DataBind();

		}
        public string ReturnString(string txt)
        {
            string str = string.Empty;
            if (txt.Trim().Length == 8)
            {
                int i;
                if (int.TryParse(txt.Trim(), out i))
                {
                    string nian = txt.Trim().Substring(0, 4);
                    string yue = txt.Trim().Substring(4, 4);
                    str = nian + "）第" + yue;
                }
                else
                {
                    str = txt;
                }
            }
            else
            {
                str = txt;
            }
            return str;

        }

		public string ReturnString2(string txt)
		{
			string str = string.Empty;
			if (txt.Trim().Length == 7)
			{
				int i;
				if (int.TryParse(txt.Trim(), out i))
				{
					string nian = txt.Trim().Substring(0, 4);
					string yue = txt.Trim().Substring(4, 3);
					str = nian + "年第" + yue;
				}
				else
				{
					str = txt;
				}
			}
			else
			{
				str = txt;
			}
			return str;
		}

    }
}


